package com.zretc.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.zretc.entity.CollectionInfo;
import com.zretc.entity.UserInfo;
import com.zretc.service.UserService;
import com.zretc.service.impl.UserServiceImpl;

/**
 * Servlet implementation class CollectionServlet
 */
/**
 * 收藏相关业务请求
 * @author liumeiqi777
 *
 */
@WebServlet("/CollectionServlet")
public class CollectionServlet extends HttpServlet {
	private UserService userService =new UserServiceImpl();
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CollectionServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op =request.getParameter("op");
		if("ifCollection".equals(op)) {//判断该民宿是否已经被收藏
			doFindifCollection(request,response);
		}else if("changeCollection".equals(op)){//对民宿进行取消收藏
			doChangeCollention(request,response);
		}
	}
    /**
     * 根据用户收藏状态对收藏按钮添加点击事件更改登录用户对民宿的收藏状态
     * @author liumeiqi
     * @param request
     * @param response
     * @throws IOException
     */
	private void doChangeCollention(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		    HttpSession session = request.getSession();
		    UserInfo userInfo = (UserInfo) session.getAttribute("user");//获取session中的user对象
			Integer userId = userInfo.getUserId();
			String homestayId = request.getParameter("homestayId");//获取传进来的民宿id
			String collectionId = homestayId + userId;//收藏id为民宿id+用户id来保证唯一性
			//首先判断该民宿是否被用户收藏
			CollectionInfo collectionInfo = new CollectionInfo(collectionId, userInfo, null);//根据用户和收藏id创建一个收藏对象
			boolean changeState = true;//为改变状态设置初始值为true，代表已被收藏
			boolean collectionState = userService.findIfCollectionByCollectionIdAndUserId(homestayId+userId, userId);//判断该民宿对于该用户的收藏状态，返回true则是已被收藏
			if(collectionState == true) {//若被收藏则删除收藏夹中此条收藏记录
				if(userService.removeCollectionInfoByStar(collectionInfo)) {
					changeState = false;//变成未被收藏的状态
				};
			}else if(collectionState == false){//若未被收藏则增加此条收藏记录
				if(userService.saveCollectionInfoByStar(collectionInfo)) {
					changeState = true;//变成被收藏的状态
				};
			}
		   response.getWriter().print(changeState);
		}
		

	private void doFindifCollection(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo) session.getAttribute("user");//获取session中的user对象
		if(userInfo == null) {
			response.getWriter().print(false);//默认未登录状态为未收藏状态
		}else {
			Integer userId = userInfo.getUserId();
			String homestayId = request.getParameter("homestayId");//获取传进来的民宿id
			boolean collectionState = userService.findIfCollectionByCollectionIdAndUserId(homestayId+userId, userId);//判断该民宿对于该用户的收藏状态，返回true则是已被收藏
			response.getWriter().print(collectionState);
		}
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
